package com.maijinjie.admin.content.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.maijinjie.common.bean.Pagination;
import com.maijinjie.common.bean.StatusBean;
import com.maijinjie.admin.content.bean.PictureItemBean;
import com.maijinjie.admin.content.bean.PictureNewsBean;
import com.maijinjie.admin.content.dao.PictureItemMapper;
import com.maijinjie.admin.content.dao.PictureNewsMapper;
import com.maijinjie.admin.content.field.PictureNewsFields;
import com.maijinjie.admin.content.service.PictureNewsService;
import com.maijinjie.common.service.impl.BaseServiceImpl;

/**
 * PictureNewsServiceImpl
 * @author 罗正杰
 **/
@Service("pictureNewsService")
public class PictureNewsServiceImpl extends BaseServiceImpl implements PictureNewsService{
	@Resource
	PictureNewsMapper pictureNewsMapper;
	
	@Resource
	PictureItemMapper pictureItemMapper;
	
	@Override
	public PictureNewsBean getBeanById(Integer id) throws Exception{
		if(null==id || 0>=id){
			return null;
		} else {
			return (PictureNewsBean) getBeanById(pictureNewsMapper, id);
		}
	}
	@Override
	public int count(Map<String, Object> params) throws Exception {
		return pictureNewsMapper.count(params);
	}
	@Override
	public List<PictureNewsBean> list(Map<String, Object> params) throws Exception {
		return (List<PictureNewsBean>) pictureNewsMapper.list(params);
	}
	@Override
	public StatusBean save(PictureNewsBean bean, String createBy, Integer createUserId) throws Exception {
		Date currDate = new Date();
		if(bean != null){
			//TODO 处理业务
			bean.setCreateUserId(createUserId);
			bean.setLastUpdateUserId(createUserId);
			bean.setCreateDate(currDate);
			bean.setLastUpdateDate(currDate);
			if(bean.getPicPath() == null || "".equals(bean.getPicPath())){
				return error("请上传图片");
			}
			if(0<pictureNewsMapper.save(bean)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public StatusBean update(PictureNewsBean bean, String createBy, Integer createUserId) throws Exception {
		if(null!=bean){
			if(bean.getPicPath() == null || "".equals(bean.getPicPath())){
				return error("请上传封面");
			}
			if(bean.getImageUrlList()!= null&& !"".equals(bean.getImageUrlList())){
				String imageUrlList = bean.getImageUrlList();
				String[] image = imageUrlList.split("####");
				if(image != null){
					for(String str : image){
						PictureItemBean item = new PictureItemBean();
						item.setParentId(bean.getId());
						item.setPicPath(str);
						pictureItemMapper.save(item);
					}
				}
			}
			Date currDate = new Date();
			HashMap<String, Object> params = new HashMap<String, Object>();
			//TODO 处理业务
			params.put("title",bean.getTitle());
			params.put("descriptionCn",bean.getDescriptionCn());
			params.put("descriptionEn",bean.getDescriptionEn());
			params.put("remark",bean.getRemark());
			params.put("picPath",bean.getPicPath());
			params.put("lastUpdateUserId",createUserId);
			params.put("lastUpdateDate",currDate);
			params.put("id",bean.getId());
			if(pictureNewsMapper.update(params)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public StatusBean deleteById(Integer id) throws Exception {
		if(null!=id && 0<id){
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put(PictureNewsFields.id.toString(), id);
			if(pictureNewsMapper.delete(params)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public Pagination<PictureNewsBean> list(Pagination<PictureNewsBean> pagination) throws Exception {
		return (Pagination<PictureNewsBean>) getPagination(pictureNewsMapper, pagination);
	}
	@Override
	public StatusBean batchDelete(String ids) throws Exception {
		if(null!=ids && !"".equals(ids.trim())) {
			HashMap<String, Object> params = new HashMap<String, Object>();
			params.put(PictureNewsFields.id.toString(), ids);
			if(pictureNewsMapper.batchDelete(params)){
				return success("操作成功！");
			}
		}
		return error("操作失败！");
	}
	@Override
	public List<PictureNewsBean> listAll() throws Exception {
		return (List<PictureNewsBean>) pictureNewsMapper.list(null);
	}
	@Override
	public List<PictureNewsBean> listWithPicList(Map<String, Object> params)
			throws Exception {
		List<PictureNewsBean> list = (List<PictureNewsBean>) pictureNewsMapper.list(params);
		if(list !=null){
			for(PictureNewsBean bean : list){
				params.clear();
				params.put("parentId", bean.getId());
				bean.setPicList((List<PictureItemBean>) this.pictureItemMapper.list(params));
			}
		}
		return list;
	}
}
